﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using SystemLYH.API.Application.Command.LocationCommand;
using SystemLYH.Domain;
using SystemLYH.Domain.Dto;

namespace SystemLYH.API.Controllers
{
    /// <summary>
    /// 库位API控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LocationController : ControllerBase
    {
        protected readonly IMediator _mediator;
        /// <summary>
        /// 中介者构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public LocationController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 库位添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateLocation(LocationCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 库位列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<Location>>> ShowLocation([FromQuery] LocationShowCommand command)
        {
            ApiResult<PageDTO<Location>> result = new ApiResult<PageDTO<Location>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 查看库位详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Location>> GetLocation([FromQuery] LocationBackfillCommand command)
        {
            ApiResult<Location> result = new ApiResult<Location>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 库位删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteLocation(LocationDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 库位修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateLocation(LocationUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 仓库详情关联库区
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<Location>>> ParticularsShowLocation([FromQuery]ParticularsShowLocationCommand request)
        {
            ApiResult<PageDTO<Location>> result = new ApiResult<PageDTO<Location>>();
            try
            {
                result.Data = await _mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 查看库位下拉
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Location>>> GetSelectLocation([FromQuery] LocationSelectShowcommand command)
        {
            ApiResult<List<Location>> result = new ApiResult<List<Location>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
